package org.mii.safeguard_v1.tools.jfreechart;

import java.awt.Color;
import java.awt.Font;
import java.util.Hashtable;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.Axis;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.mii.safeguard_v1.tools.jfreechart.utilities.DatasetUtilities;

public class DrawChartService {
	/**
	 * 绘制3D柱状图
	 * 
	 * @param data
	 * @param listRows：List <T>
	 * @param listCols：List <T>
	 * @param title
	 * @return
	 */
	public static JFreeChart createBar3D(double[][] data,
			Hashtable listRows, Hashtable listCols,
			String title) {
		CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
				listRows, listCols, data);
		JFreeChart chart = ChartFactory.createBarChart3D(title, null, null,
				dataset, PlotOrientation.VERTICAL, true, false, false);
		chart.setBackgroundPaint(Color.WHITE);
		CategoryPlot plot = chart.getCategoryPlot();

		CategoryAxis domainAxis = plot.getDomainAxis();
		plot.setDomainAxis(domainAxis);

	//	Axis rangeAxis = plot.getRangeAxis();
		BarRenderer3D renderer = new BarRenderer3D();
		renderer.setBaseOutlinePaint(Color.BLACK);
		// 设置 Wall 的颜色
		renderer.setWallPaint(Color.gray);
		// 设置每种水果代表的柱的颜色
		renderer.setSeriesPaint(0, new Color(0, 0, 255));
		renderer.setSeriesPaint(1, new Color(255, 0, 255));
		renderer.setSeriesPaint(2, Color.GREEN);
		// 设置每个地区所包含的平行柱的之间距离
		renderer.setItemMargin(0.1);
		// 显示每个柱的数值，并修改该数值的字体属性
		renderer
				.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		renderer.setItemLabelsVisible(true);
		plot.setRenderer(renderer);

		// 设置柱的透明度
		plot.setForegroundAlpha(0.6f);
		// 设置地区、销量的显示位置
		//plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
		//plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
		return chart;
	}

	/**
	 * 绘制3D柱状图
	 * 
	 * @param data
	 * @param listRows：String[]
	 * @param listCols：String[]
	 * @param title
	 * @return
	 */
	public static JFreeChart createBar3D(double[][] data, String[] listRows,
			String[] listCols, String title) {
		CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
				listRows, listCols, data);
		JFreeChart chart = ChartFactory.createBarChart3D(title, null, null,
				dataset, PlotOrientation.VERTICAL, true, false, false);
		chart.setBackgroundPaint(Color.WHITE);
		CategoryPlot plot = chart.getCategoryPlot();

		CategoryAxis domainAxis = plot.getDomainAxis();
		plot.setDomainAxis(domainAxis);

		Axis rangeAxis = plot.getRangeAxis();
		BarRenderer3D renderer = new BarRenderer3D();
		renderer.setBaseOutlinePaint(Color.BLACK);
		// 设置 Wall 的颜色
		renderer.setWallPaint(Color.gray);
		// 设置每种水果代表的柱的颜色
		renderer.setSeriesPaint(0, new Color(0, 0, 255));
		renderer.setSeriesPaint(1, new Color(0, 100, 255));
		renderer.setSeriesPaint(2, Color.GREEN);
		// 设置每个地区所包含的平行柱的之间距离
		renderer.setItemMargin(0.1);
		// 显示每个柱的数值，并修改该数值的字体属性
		renderer
				.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		renderer.setItemLabelsVisible(true);
		plot.setRenderer(renderer);

		// 设置柱的透明度
		plot.setForegroundAlpha(0.6f);
		// 设置地区、销量的显示位置
		plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
		plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
		return chart;
	}
	public static JFreeChart createBar3D(int[] data, String[] listRows,
			String[] listCols, String title) {
		CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
				listRows, listCols, data);
		JFreeChart chart = ChartFactory.createBarChart3D(title, null, null,
				dataset, PlotOrientation.VERTICAL, true, false, false);
		chart.setBackgroundPaint(Color.WHITE);
		CategoryPlot plot = chart.getCategoryPlot();
		
		CategoryAxis domainAxis = plot.getDomainAxis();
		plot.setDomainAxis(domainAxis);
		
		Axis rangeAxis = plot.getRangeAxis();
		BarRenderer3D renderer = new BarRenderer3D();
		renderer.setBaseOutlinePaint(Color.BLACK);
		// 设置 Wall 的颜色
		renderer.setWallPaint(Color.gray);
		// 设置每种水果代表的柱的颜色
		//renderer.setSeriesPaint(0, new Color(0, 0, 255));
		//renderer.setSeriesPaint(1, new Color(0, 100, 255));
		//renderer.setSeriesPaint(2, Color.GREEN);
		// 设置每个地区所包含的平行柱的之间距离
		renderer.setItemMargin(0.1);
		// 显示每个柱的数值，并修改该数值的字体属性
		renderer
		.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		renderer.setItemLabelsVisible(true);
		plot.setRenderer(renderer);
		
		// 设置柱的透明度
		plot.setForegroundAlpha(0.6f);
		// 设置地区、销量的显示位置
		plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
		plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
		return chart;
	}
/**
 * 绘制饼状图
 * @param dataset:DefaultPieDataset
 * @param title:String
 * @return
 */
	public static JFreeChart createPie(DefaultPieDataset dataset, String title) {
		JFreeChart chart = ChartFactory.createPieChart(
				title, // chart title
				dataset, // data
				true, // include legend
				true, false);
		PiePlot plot = (PiePlot) chart.getPlot();
		plot.setSectionOutlinesVisible(false);
		plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
		plot.setNoDataMessage("没有可供使用的数据！");
		plot.setCircular(false);
		plot.setLabelGap(0.02);
       // plot.setExplodePercent(1, 0.50); //用来抽取出一块扇形
		
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
                "{0} ({2} percent)"));
        plot.setLabelBackgroundPaint(new Color(220, 220, 220));
        
        plot.setLegendLabelToolTipGenerator(
                new StandardPieSectionLabelGenerator(
                        "Tooltip for legend item {0}"));
		return chart;
	}
	/**
	 * 绘制柱状图
	 * @param dataset:DefaultPieDataset
	 * @param title:String
	 * @return
	 */
	/*
		public static JFreeChart createPieChart(DefaultPieDataset dataset, String mytitle) {
			JFreeChart chart = ChartFactory.createPieChart(mytitle,dataset,true, true, false);
			//PiePlot plot = (PiePlot) chart.getPlot();
			//plot.setSectionOutlinesVisible(false);
			//plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
			//plot.setNoDataMessage("没有可供使用的数据！");
			//plot.setCircular(false);
			//plot.setLabelGap(0.02);
	
	       // plot.setExplodePercent(1, 0.50); //用来抽取出一块扇形
			TextTitle title = chart.getTitle();
	        title.setToolTipText("A title tooltip!");
	        
	        PiePlot plot = (PiePlot) chart.getPlot();
	        plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
	        plot.setNoDataMessage("No data available");
	        plot.setCircular(false);
	        plot.setLabelGap(0.02);
			
			
	        
	        plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
	                "{0}{1}条({2})"));
	    //    plot.setLabelBackgroundPaint(new Color(220, 220, 220));
	        
	   //     plot.setLegendLabelToolTipGenerator(
	   //             new StandardPieSectionLabelGenerator(
	   //                     "Tooltip for legend item {0}"));
			return chart;
		}
	*/
	
	/**
	 * 绘制柱状图
	 * @param dataset:DefaultPieDataset
	 * @param title:String
	 * @return
	 */
		public static JFreeChart createPieChart(DefaultPieDataset dataset, String mytitle) {
			JFreeChart chart = ChartFactory.createPieChart(mytitle,dataset,true, true, false);
			TextTitle title = chart.getTitle();
	        title.setToolTipText("A title tooltip!");
	        
	        PiePlot plot = (PiePlot) chart.getPlot();
	        plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
	        plot.setNoDataMessage("没有可供使用的数据");
	        plot.setCircular(false);
	        plot.setLabelGap(0.02);
			
			plot.setSectionPaint("审核已通过", Color.GREEN);
			plot.setSectionPaint("未提交", Color.BLUE);
			plot.setSectionPaint("已提交", Color.YELLOW);
			plot.setSectionPaint("审核未通过", Color.RED);
			//add by quanxiwei 20081007
	        
	        plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
	                "{0}{1}个({2})"));//将“条”改为了“个”
			return chart;
		}
}

